package com.example.demo.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Customer;
import com.example.demo.mapper.BookingMapper;
import com.example.demo.mapper.CustomerMapper;
import com.example.demo.mapper.RoomMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/customer")
public class CustomerController {
    @Resource
    CustomerMapper customerMapper;

    @Autowired
    @Resource
    BookingMapper bookingMapper;

    @Autowired
    @Resource
    RoomMapper roomMapper;


    @PostMapping("/add")
    public Result<?> add(@RequestBody Customer customer){
        //1.数据插入用户表
        customerMapper.insert(customer);
        //2.更新预定表的状态
        bookingMapper.updateStateByBkId(customer.getBkId());
        //3.更新房间的状态
        roomMapper.updateState1ByRoomId(customer.getRoomId());

        return Result.success();
    }

    @GetMapping("/data")
    public Result<?> test(@RequestParam Integer pageNum,
                          @RequestParam Integer pageSize,
                          @RequestParam String search){
        LambdaQueryWrapper<Customer> wrapper = Wrappers.<Customer>lambdaQuery();
        if (StrUtil.isNotBlank(search)) wrapper.like(Customer::getName,search);
        Page<Customer> userPage = customerMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(userPage);
    }

    @PutMapping("/renew")
    public Result<?> renew(@RequestBody Customer customer){
        //1.修改用户的退房时间
        customerMapper.updateById(customer);
        return Result.success("续住成功！");
    }
}
